/**
*      author: Y_Yao
*      created: 2024-09-22 09:36:39
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const int INF = 0x3f3f3f3f;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
// 东 南 西 北
// 0  1  2  3
int turnleft[4] = {3,0,1,2}; 
int turnright[4] = {1,2,3,0}; 
int a[55][55];
bool vis[55][55][4];
int N,M;
int sx,sy,ex,ey,ret;
char sf;

struct node
{
    int x,y,time,face;
};

void bfs(node& start)
{
    queue<node> q;
    q.push(start);
    vis[start.x][start.y][start.face] = true;

    while(!q.empty())
    {
       
        node tmp = q.front();
        q.pop();


        if(tmp.x == ex && tmp.y == ey)
        {
            ret = min(ret,tmp.time);
        }

        node newnode1 = tmp;
        newnode1.time++;
        for(int i = 1;i <= 3;i++)
        {
            newnode1.x += dx[newnode1.face];
            newnode1.y += dy[newnode1.face];
            if(newnode1.x<1 || newnode1.x>=N||newnode1.y<1||newnode1.y>=M||a[newnode1.x][newnode1.y])
                break;
            else if(vis[newnode1.x][newnode1.y][newnode1.face])
                continue;
            q.push(newnode1);
            vis[newnode1.x][newnode1.y][newnode1.face] = true;
        }
        node newnode = tmp;
        newnode.time++;

        //左转
        newnode.face = turnleft[tmp.face];
        if(!vis[newnode.x][newnode.y][newnode.face])
        {    
            q.push(newnode);
            vis[newnode.x][newnode.y][newnode.face] = true;
        }

        //右转
        newnode.face = turnright[tmp.face];
        if(!vis[newnode.x][newnode.y][newnode.face])
        {    
            q.push(newnode);
            vis[newnode.x][newnode.y][newnode.face] = true;
        }

    }


    if(ret == INF) cout << "-1" << endl;
    else cout << ret << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    ret = INF;
    cin>>N>>M;
    for(int i = 1;i <= N; i++)
    {
        for(int j = 1;j <= M;j++)
        {
            int t = 0;
            cin >> t;
            if(t == 1)
            {
                a[i][j-1] = a[i-1][j] = a[i-1][j-1] = a[i][j] = 1;
            }
        }
    }

    cin >> sx >> sy >> ex >> ey >> sf;
    node start;
    start.x = sx;
    start.y = sy;
    start.time = 0;
    if(sf == 'E')  start.face = 0;
    else if(sf == 'S') start.face = 1;
    else if(sf == 'W') start.face = 2;
    else start.face = 3;

    bfs(start);

    return 0;
}